package cn.medemede.servlet.action;


import cn.medemede.dao.UserpwdDao;
import cn.medemede.dao.UsersDao;
import cn.medemede.model.UserpwdBean;
import cn.medemede.model.UsersBean;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;


public class Do_join implements Action{

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response) throws Exception {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=UTF-8");
        UserpwdBean myUser=new UserpwdBean();
        UsersBean myUserInfo=new UsersBean();
        PrintWriter out=response.getWriter();
        String farward="join_failed";
        String Register_name=request.getParameter("username");
        System.out.println(Register_name);
        String Register_password=request.getParameter("password");
        System.out.println(Register_password);
        String character=request.getParameter("character");
        String zname=request.getParameter("zname");
        String sex=request.getParameter("sex");
        String borth=request.getParameter("borth");
        String phone=request.getParameter("phone");
        String zposition=request.getParameter("province")+" "+request.getParameter("city");
        String email=request.getParameter("email");
        String[] favorite=request.getParameterValues("favorite");
        String favorite_s=" ";
        if(favorite!=null) {
            for (String s : favorite) {
                favorite_s += s + " ";
            }
        }
        System.out.println(favorite_s);
        System.out.println(character);
        myUser.setUsername(Register_name);
        myUser.setPassword(Register_password);

        myUserInfo.setUsername(Register_name);
        myUserInfo.setZname(zname);
        myUserInfo.setSex(sex);
        myUserInfo.setBorth(borth);
        myUserInfo.setPhone(phone);
        myUserInfo.setZposition(zposition);
        myUserInfo.setEmail(email);
        myUserInfo.setFavorite(favorite_s);
        myUserInfo.setCharacter(character);
        if(UsersDao.creat(myUserInfo)&& UserpwdDao.creat(myUser)){
            out.println("注册成功！");
            farward="join_suc";
        }else {
            out.println("注册失败！");
            farward="join_failed";
        }
        return farward;
    }
}
